package cn.com.nes.site.service.system.company.impl;

import cn.com.nes.mybatis.agent.system.entity.CompanyMerchant;
import cn.com.nes.mybatis.agent.system.mapper.CompanyMerchantMapper;
import cn.com.nes.site.entity.vo.user.SaveBusinessCompanyVo;
import cn.com.nes.site.service.system.company.CompanyMerchantService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CompanyMerchantServiceImpl extends ServiceImpl<CompanyMerchantMapper, CompanyMerchant> implements CompanyMerchantService {

    @Autowired
    CompanyMerchantMapper companyMerchantMapper;

    @Override
    public void processCompanyMerchant(String companyId, SaveBusinessCompanyVo saveBusinessCompanyVo) {
        // 查询公司与商铺关系
        CompanyMerchant companyMerchants = selectByCompanyMerchantId(companyId, saveBusinessCompanyVo.getMerchantId());

        if (ObjectUtil.isNull(companyMerchants)) {
            // 不存在，建立公司与商铺关系
            CompanyMerchant companyMerchant = new CompanyMerchant();
            companyMerchant.setCompanyId(companyId);
            companyMerchant.setMerchantId(saveBusinessCompanyVo.getMerchantId());
            companyMerchant.setMerchantName(saveBusinessCompanyVo.getCompanyFullName());
            companyMerchant.setMerchantRealname(saveBusinessCompanyVo.getCompanyShortName());
            companyMerchant.setMerchantPhone(saveBusinessCompanyVo.getContact());
            companyMerchant.setGmtCreate(new Date());

            insert(companyMerchant);
        }
    }

    @Override
    public String queryCompanyIdByMerId(Integer merId) {
        if (ObjectUtil.isNull(merId)) {
            return StrUtil.EMPTY;
        }
        QueryWrapper<CompanyMerchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("company_id");
        queryWrapper.eq("merchant_id", merId);
        queryWrapper.last("limit 1");
        List<CompanyMerchant> companyMerchants = companyMerchantMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(companyMerchants)) {
            return StrUtil.EMPTY;
        }
        return companyMerchants.get(0).getCompanyId();
    }

    @Override
    public CompanyMerchant selectByCompanyMerchantId(String companyId, String merchantId) {
        LambdaQueryWrapper<CompanyMerchant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(companyId), CompanyMerchant::getCompanyId, companyId);
        queryWrapper.eq(StringUtils.isNotEmpty(merchantId), CompanyMerchant::getMerchantId, merchantId);

        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void insert(CompanyMerchant companyMerchant) {
        companyMerchantMapper.insert(companyMerchant);
    }

}
